package com.struts.deal.admin.popedom;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.base.Constants;
import com.ibatis.po.system.TPopecode;
import com.ibatis.po.system.TPopedom;
import com.ibatis.po.system.TSysAdmin;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.lg.system.CSqlMap;
import com.lg.system.CTurnPage;
import com.lg.system.Common;
import com.lg.system.MessageBean;
import com.struts.web.manage.admin.popedom.AllotPopeForm;
import com.struts.web.manage.admin.popedom.PopedomListForm;
import com.struts.web.manage.admin.popedom.UserManageForm;

public class PopedomDeal {


	/**
	 * 用户浏览列表
	 * @param request
	 * @param aWebForm
	 */
	public static void viewUser(HttpServletRequest request,UserManageForm aWebForm){

		CTurnPage page = new CTurnPage();
		TSysAdmin userPo = new TSysAdmin();
		try{
			userPo.setName(aWebForm.getName());
			userPo.setRoleDescript(aWebForm.getRoleDescript());
			userPo.setStatus(aWebForm.getStatus());

			page.setQueryList("T_SYS_ADMIN.selectAllByCondition",userPo);
			page.setPerPageRows(10);
			page.excute(request);
			request.setAttribute("page", page);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * 权限列表
	 * @param request
	 * @param aWebForm
	 */
	public static void popedomList(HttpServletRequest request,PopedomListForm aWebForm){

		CTurnPage page = new CTurnPage();
		TPopedom popePo = new TPopedom();
		try{
			popePo.setPopedomDescript(aWebForm.getPopedomDescript());
			popePo.setStatus(aWebForm.getStatus());
			popePo.setName(aWebForm.getName());

			page.setQueryList("T_POPEDOM.selectAllByCondition",popePo);
			page.setPerPageRows(10);
			page.excute(request);
			request.setAttribute("page", page);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * 添加管理用户
	 * @param request
	 * @param aWebForm
	 */
	public static MessageBean addUser(HttpServletRequest request,UserManageForm aWebForm) throws SQLException{
		MessageBean messageBean = new MessageBean();
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		TSysAdmin userPo = new TSysAdmin();
		int count = 0;
		String longiName = "";
		try{
			if(aWebForm.getLoginName()!=null){
				longiName = aWebForm.getLoginName();

			}

			if(longiName.length()>0){
				count = ((Integer)sqlmap.queryForObject("T_SYS_ADMIN.selectIsExist")).intValue();
				if(count<1){
					userPo.setId(Common.getNewID());
					userPo.setName(aWebForm.getName());
					userPo.setLoginName(longiName);
					userPo.setRoleDescript(aWebForm.getRoleDescript());
					userPo.setStatus(aWebForm.getStatus());
					userPo.setCreateTime(new java.util.Date());
					userPo.setPassword(aWebForm.getPassword());
					sqlmap.insert("T_SYS_ADMIN.abatorgenerated_insert",userPo);
					messageBean.setResult(true);
				}else{
					messageBean.setMessage(Constants.USER_EXIST);
				}
			}

		}catch(Exception e){
			messageBean.setMessage(Constants.ADD_FAIL);
			e.printStackTrace();
		}
		return messageBean;
	}

	/**
	 * 修改管理用户
	 * @param request
	 * @param aWebForm
	 */
	public static MessageBean editUser(HttpServletRequest request,UserManageForm aWebForm) throws SQLException{
		MessageBean messageBean = new MessageBean();
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		TSysAdmin userPo = new TSysAdmin();
		String id = request.getParameter("adminUserId");
		try{
			if(id!=null && id!=""){
				userPo.setId(id);
				userPo.setLoginName(aWebForm.getLoginName());
				userPo.setPassword(aWebForm.getPassword());
				userPo.setRoleDescript(aWebForm.getRoleDescript());
				userPo.setStatus(aWebForm.getStatus());
				userPo.setName(aWebForm.getName());

				sqlmap.update("T_SYS_ADMIN.abatorgenerated_updateByPrimaryKeySelective",userPo);
				messageBean.setResult(true);
			}

		}catch(Exception e){
			messageBean.setMessage(Constants.EDIT_FAIL);
			e.printStackTrace();
		}
		return messageBean;
	}

	/**
	 * 加载管理用户信息
	 * @param request
	 * @param aWebForm
	 */
	public static MessageBean loadUser(HttpServletRequest request,UserManageForm aWebForm) throws SQLException{
		MessageBean messageBean = new MessageBean();
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		TSysAdmin userPo = new TSysAdmin();
		String id = request.getParameter("adminUserId");
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
		try{
			userPo = (TSysAdmin)sqlmap.queryForObject("T_SYS_ADMIN.selectById",id);

			if(userPo!=null){
				aWebForm.setId(userPo.getId());
				aWebForm.setLoginName(userPo.getLoginName());
				aWebForm.setName(userPo.getName());
				aWebForm.setRoleDescript(userPo.getRoleDescript());
				aWebForm.setStatus(userPo.getStatus());
				aWebForm.setCreateTime(ft.format(userPo.getCreateTime()));
				messageBean.setResult(true);
			}else{
				messageBean.setMessage(Constants.USER_NOTEXIST);
			}


		}catch(Exception e){
			e.printStackTrace();
		}
		return messageBean;
	}

	/**
	 * 查询用户列表
	 * @param request
	 * @param aWebForm
	 */
	public static void loadAllotPope(HttpServletRequest request,AllotPopeForm aWebForm){

		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		List userList = null;
		try{
			userList= sqlmap.queryForList("T_SYS_ADMIN.selectAll");
			request.setAttribute("userList", userList);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * 添加权限
	 * @param request
	 * @param aWebForm
	 */
	public static MessageBean addPope(HttpServletRequest request,AllotPopeForm aWebForm) throws SQLException {
		MessageBean messageBean = new MessageBean();
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		TSysAdmin userPo = new TSysAdmin();
		String popeStr = aWebForm.getPopeList();
		TPopedom popedomPo = new TPopedom();
		String[] popeArry = new String[40];
		int count = 0;
		try{
			if(popeStr!=null && popeStr!=""){
				popeArry = popeStr.split(";");
			}

			String userId  = aWebForm.getUserId();
			if(userId!=null && userId!=""){

				popedomPo.setUserId(userId);
				popedomPo.setPopedomDescript(aWebForm.getPopeDescript());
				popedomPo.setId(Common.getNewID());
				sqlmap.delete("T_POPECODE.deleteByUserId",userId);

				count = ((Integer)sqlmap.queryForObject("T_POPEDOM.selectIsExist",userId)).intValue();
				if(count<1){
					sqlmap.insert("T_POPEDOM.abatorgenerated_insert",popedomPo);
				}else{
					sqlmap.update("T_POPEDOM.updateByUserId",popedomPo);
				}
				

				for(int i=0;i<popeArry.length;i++){
					TPopecode popeCodePo = new TPopecode();
					popeCodePo.setId(Common.getNewID());
					popeCodePo.setCode(popeArry[i]);
					popeCodePo.setUserId(userId);
					sqlmap.insert("T_POPECODE.abatorgenerated_insert",popeCodePo);
				}
				messageBean.setResult(true);
			}else{
				messageBean.setMessage(Constants.USER_NOTEXIST);
			}
			
		}catch(Exception e){
			messageBean.setMessage(Constants.ADD_FAIL);
			e.printStackTrace();
		}
		return messageBean;
	}

	/**
	 * 加载指定用户的权限信息
	 * @param request
	 * @param aWebForm
	 */
	public static MessageBean loadUserPope(HttpServletRequest request,AllotPopeForm aWebForm){
		MessageBean messageBean = new MessageBean();
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		List userList = null;
		TPopedom popedomPo = new TPopedom();
		String userId = request.getParameter("adminUserId");
		List mpopeList = null;
		try{
			popedomPo = (TPopedom)sqlmap.queryForObject("T_POPEDOM.selectInfoByUserId",userId);
			aWebForm.setPopeDescript(popedomPo.getPopedomDescript());
			aWebForm.setStatus(popedomPo.getStatus());
			aWebForm.setUserId(userId);
			
			mpopeList = sqlmap.queryForList("T_POPECODE.selectById",userId);
			System.out.println("send:"+mpopeList.size());
			userList= sqlmap.queryForList("T_SYS_ADMIN.selectAll");
			request.setAttribute("userList", userList);
			request.setAttribute("mpopeList", mpopeList);
			messageBean.setResult(true);
		}catch(Exception e){
			messageBean.setMessage(Constants.LOAD_FAIL);
			e.printStackTrace();
		}
		return messageBean;
	}
	
	/**
	 * 删除管理用户
	 * @param request
	 * @param aWebForm
	 */
	public static MessageBean delUser(HttpServletRequest request,UserManageForm aWebForm) throws SQLException{
		MessageBean messageBean = new MessageBean();
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		String userId = request.getParameter("adminUserId");
		try{
			sqlmap.queryForObject("F_DEL_TABLE.delAllAboutAdmin",userId);
			messageBean.setResult(true);
		}catch(Exception e){
			messageBean.setMessage(Constants.DEL_FAIL);
			e.printStackTrace();
		}
		return messageBean;
	}
}
